#ifndef _MSG_PACK_TRANSFER_H_
#define _MSG_PACK_TRANSFER_H_

#include <vector>
#include <conn_pool.h>
#include <csmsg_protocol/msg.h>

const int MAX_TCP_CODE_LENGTH = 32 * 1024 - 1;
const int MAX_MSG_TRANSFER_TIMEOUT = 3;
const int MAX_INACTIVE_CONN_TIMEOUT = 60;
const int MAX_TRANSFER_ERRMSG_LENGTH = 512;

class csmsg
{
public:
    CCSHead cshead_;
    CMsg msg_;
};

typedef bool (*followup_pack_checker)(const CMsg &msg);

class csmsg_transfer
{
public:
    void initialize();
    int add_service(const char* ip, unsigned short port);
    int prepare_transfer();
    int send_csmsg(CCSHead &cshead, CMsg &msg);
    int recv_csmsg(CCSHead *cshead, CMsg *msg);
    int recv_multi_csmsg(std::vector<csmsg> *msgs, followup_pack_checker is_last_pack);
    const char* get_errmsg() { return errmsg_; }
private:
    ezycli::conn_pool conns_;
    ezycli::conn_with_credit *pick_conn_;
    char code_buffer_[MAX_TCP_CODE_LENGTH];

    char errmsg_[MAX_TRANSFER_ERRMSG_LENGTH];
};

#endif
